﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IronCow.UnitTests.Mock
{
    public class MockTaskSeriesResponsePart : MockResponsePart
    {
        public string SeriesId { get; set; }
        public DateTime Created { get; set; }
        public DateTime? Modified { get; set; }
        public string Name { get; set; }
        public string Source { get; set; }
        public string Url { get; set; }
        public int? LocationId { get; set; }
        public string[] Tags { get; set; }
        public MockResponsePartCollection Notes { get; private set; }

		public MockTaskSeriesResponsePart(int seriesId, string name)
            : this(seriesId, name, DateTime.Today)
        {
        }

		public MockTaskSeriesResponsePart(int seriesId, string name, string url)
            : this(seriesId, name, DateTime.Today, url)
        {
        }

		public MockTaskSeriesResponsePart(int seriesId, string name, DateTime created)
            : this(seriesId, name, created, string.Empty)
        {
        }

		public MockTaskSeriesResponsePart(int seriesId, string name, DateTime created, string url)
            : this(seriesId, name, created, url, new string[0])
        {
        }

		public MockTaskSeriesResponsePart(int seriesId, string name, DateTime created, string url, params string[] tags)
        {
			SeriesId = seriesId.ToString();
            Name = name;
            Created = created;
            Modified = created;
            Source = "api";
            Url = url;
            if (tags != null)
            {
                Tags = new string[tags.Length];
                tags.CopyTo(Tags, 0);
            }
            Notes = new MockResponsePartCollection();
        }

        public override void GetResponseMarkup(System.Xml.XmlWriter markupWriter, Dictionary<string, string> parameters)
        {
            markupWriter.WriteStartElement("taskseries");
            markupWriter.WriteAttributeString("id", SeriesId.ToString());
            markupWriter.WriteAttributeString("created", Created.ToString("s"));
            if (Modified.HasValue) markupWriter.WriteAttributeString("modified", Modified.Value.ToString("s"));
            markupWriter.WriteAttributeString("name", Name);
            markupWriter.WriteAttributeString("source", Source);

            if (parameters["method"] == "rtm.tasks.setURL")
                markupWriter.WriteAttributeString("url", parameters["url"]);
            else
                markupWriter.WriteAttributeString("url", Url);

            if (LocationId.HasValue) markupWriter.WriteAttributeString("location_id", LocationId.Value.ToString());

            markupWriter.WriteStartElement("tags");
            if (Tags != null)
            {
                foreach (var tag in Tags)
                {
                    markupWriter.WriteElementString("tag", tag);
                }
            }
            markupWriter.WriteEndElement();

            markupWriter.WriteStartElement("notes");
            foreach (var part in Notes)
            {
                part.GetResponseMarkup(markupWriter, parameters);
            }
            markupWriter.WriteEndElement();

            GetNestedResponseMarkup(markupWriter, parameters);

            markupWriter.WriteEndElement();
        }
    }
}
